package com.horoscope.astrology.zodiac.signs.free.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.PreMainActivity
import com.horoscope.astrology.zodiac.signs.free.utils.StringUtils

/**
 *  用户名
 * */
class NameFragment : BaseFragment() {
    private var mEnter: ImageView? = null
    private var mEditText: EditText? = null
    private var mReplyLinear: LinearLayout? = null
    private var mReplyText: TextView? = null
    private var mEnterLinear: LinearLayout? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_name, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mRoot = view.findViewById(R.id.linear_root)
        mEnter = view.findViewById(R.id.image_view_enter)
        mEditText = view.findViewById(R.id.edit_text_name)
        mReplyLinear = view.findViewById(R.id.linear_layout_reply)
        mReplyText = view.findViewById(R.id.text_view_reply)
        mEnterLinear = view.findViewById(R.id.linear_layout_enter)
        mEditText?.requestFocus()
        val fadeIn = AnimationUtils.loadAnimation(activity, R.anim.fade_in)
        val fadeOut = AnimationUtils.loadAnimation(activity, R.anim.fade_out)
        mEnter?.setOnClickListener {
            if (mEditText?.text!!.toString().trim().isNotEmpty()) {
                val name = mEditText?.text.toString().replace("\n", " ").trim()
                val myName = StringUtils.newLineStr(name)
                hideInput()
                mEnterLinear?.visibility = View.GONE
                mReplyText?.text = myName
                (activity as PreMainActivity).setUserName(name)
                mReplyLinear?.startAnimation(fadeIn)
            }
        }
        fadeIn.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                hideInput()
                mRoot?.startAnimation(fadeOut)
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
        fadeOut.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                nextPage()
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
    }


    private fun nextPage() {
        if (activity is PreMainActivity) {
            (activity as PreMainActivity).setPage(1)
        }
    }


}